/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "device_profile_agent.h"

#include <string>

#include ""
#include "utils_log.h"

namespace OHOS {
namespace Storage {
namespace DistributedFile {

constexpr const char *SERVICE_ID = "distributedFileDaemonService";
constexpr const char *STATIC_CHARACTER_ID = "static_capability";
constexpr const char *SWITCH_STATE = "DFSSwitchState";
constexpr const char *CHARACTERISTIC_VALUE = "characteristicValue";
constexpr const char *SWITCH_ID = "SwitchStatus_Distributed_File_Daemon";
constexpr const char *CHARACTER_ID = "SwitchStatus";
constexpr int32_t DISTRIBUTED_FILE_DAEMON_SA_ID = 5201;

bool DeviceProfileAgent::GetDeviceSwitchState(const std::string &udid, SwitchState &switchState)
{
    if (udid.empty()) {
        LOGE("udid is empty");
        return false;
    }
    DistributedDeviceProfile::CharacteristicProfile profile;
    int32_t ret = DistributedDeviceProfileClient::GetInstance().GetCharacteristicProfile(udid,
        SWITCH_ID, CHARACTER_ID, profile);
    if (ret != DistributedDevcieProfile::DP_SUCCESS) {
        LOGE("get remote device switch state failed");
        return false;
    }
    const std::string switchStateStr = profile.GetCharacteristicValue();
    if (switchStateStr == "0") {
        switchState = SwitchState::OFF_STATE;
    } else if (switchStateStr == "1") {
        switchState = SwitchState::ON_STATE;
    }
    return true;
}

bool DeviceProfileAgent::SetDeviceSwitchState(const std::string &udid, const SwitchState switchState)
{
    if (udid.empty()) {
        LOGE("udid is empty");
        return false;
    }
    DistributedDeviceProfile::CharacteristicProfile profile;
    profile.SetDeviceId(udid);
    profile.SetServiceName(SWITCH_ID);
    profile.SetCharacteristicKey(CHARACTER_ID);
    profile.SetCharacteristicValue(switchState);
    int32_t ret = DistributedDeviceProfileClient::GetInstance().PutCharacteristicProfile(profile);
    if (ret != DistributedDevcieProfile::DP_SUCCESS) {
        LOGE("set remote device switch state failed");
        return false;
    }
    return static_cast<int32_t>(PasteboardError::E_OK);
}

void DeviceProfileAgent::SubscribeProfileEvent(const std::string &udid)
{
    std::lock_guard lock(mutex_);
    SubscribeInfo subscribeInfo;
    subscribeInfo.SetSaId(DISTRIBUTED_FILE_DAEMON_SA_ID);
    subscribeInfo.SetSubscribeKey(udid, SWITCH_ID, CHARACTER_ID, CHARACTERISTIC_VALUE);
    subscribeInfo.AddProfileChangeType(ProfileChangeType::CHAR_PROFILE_ADD);
    subscribeInfo.AddProfileChangeType(ProfileChangeType::CHAR_PROFILE_UPDATE);
    subscribeInfo.AddProfileChangeType(ProfileChangeType::CHAR_PROFILE_DELETE);
    sptr<IProfileChangeListener> subscribeDPChangeListener = new (std::nothrow) SubScribeDPChangeListener;
    if (subscribeDPChangeListener == nullptr) {
        LOGE("New subscribeDPChangeListener failed");
        return;
    }
    subscribeInfo.SetListener(subscribeDPChangeListener);
    subscribeInfoCache_.emplace(udid, subscribeInfo);

    int32_t ret = DistributedDeviceProfileClient::GetInstance().SubscribeDeviceProfile(subscribeInfo);
    if (ret != DistributedDevcieProfile::DP_SUCCESS) {
        LOGE("Set subscribe Info failed");
        return false;
    }
    LOGE("Unset subscribe udid: %{public}.5s success", udid.c_str());
    return static_cast<int32_t>(PasteboardError::E_OK);
}

void DeviceProfileAgent::UnSubscribeProfileEvent(const std::string &udid)
{
    std::lock_guard lock(mutex_);
    auto it = subscribeInfoCache_.find(udid);
    if (it == subscribeInfoCache_.end()) {
        LOGE("UnSubscribeProfileEvent err, can not find udid: %{public}.5s", udid.c_str());
        return;
    }

    int32_t ret = DistributedDeviceProfileClient::GetInstance().UnSubscribeDeviceProfile(it->second);
    if (ret != DistributedDevcieProfile::DP_SUCCESS) {
        LOGE("Unset subscribe Info failed");
        return false;
    }
    subscribeInfoCache_.erase(it);
    LOGE("Unset subscribe udid: %{public}.5s success", udid.c_str());
    return static_cast<int32_t>(PasteboardError::E_OK);
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnTrustDeviceProfileAdd(
    [[maybe_unused]] const TrustDeviceProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnTrustDeviceProfileDelete(
    [[maybe_unused]] const TrustDeviceProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnTrustDeviceProfileUpdate(
    [[maybe_unused]] const TrustDeviceProfile& oldProfile, [[maybe_unused]] const TrustDeviceProfile& newProfile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnTrustDeviceProfileActive(
    [[maybe_unused]] const TrustDeviceProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnTrustDeviceProfileInactive(
    [[maybe_unused]] const TrustDeviceProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnDeviceProfileAdd(
    [[maybe_unused]] const DeviceProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnDeviceProfileDelete(
    [[maybe_unused]] const DeviceProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnDeviceProfileUpdate(
    [[maybe_unused]] const DeviceProfile& oldProfile, [[maybe_unused]] const DeviceProfile& newProfile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnServiceProfileAdd(
    [[maybe_unused]] const ServiceProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnServiceProfileDelete(
    [[maybe_unused]] const ServiceProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnServiceProfileUpdate(
    [[maybe_unused]] const ServiceProfile& oldProfile, [[maybe_unused]] const ServiceProfile& newProfile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnCharacteristicProfileAdd(
    [[maybe_unused]] const CharacteristicProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnCharacteristicProfileDelete(
    [[maybe_unused]] const CharacteristicProfile& profile)
{
    LOGD("called");
    return NO_ERROR;
}

int32_t DeviceProfileAgent::SubScribeDPChangeListener::OnCharacteristicProfileUpdate(
    [[maybe_unused]] const CharacteristicProfile& oldProfile, [[maybe_unused]] const CharacteristicProfile& newProfile)
{
    LOGD("called");
    return NO_ERROR;
}} // namespace DistributedFile
} // namespace Storage
} // namespace OHOS
